using System;
using System.Collections.Generic;
using System.Text;

namespace Sucrose.Base
{
    public class TaskItem : DownloadableItem
    {
        #region instanceVars

        private TaskItem     parentTask;
        private bool         isParent = false;
        private bool         isChild = false;
        private int          tempParentTask;
        private TaskCategory category;
        private int          tempCategory;
        private DateTime     dueDate;

        private double gradeWeight;
        private double grade = -1;

        #endregion

        #region basicProperties

        public TaskItem ParentTask
        {
            get
            {
                return parentTask;
            }
            set
            {
                parentTask = value;
            }
        }

        public int TempParentTask
        {
            get
            {
                return tempParentTask;
            }
            set
            {
                tempParentTask = value;
            }
        }

        public bool IsParent
        {
            get
            {
                return isParent;
            }
            set
            {
                isParent = value;
                isChild = !value;
            }
        }

        public bool IsChild
        {
            get
            {
                return isChild;
            }
            set
            {
                isChild = value;
                isParent = !value;
            }
        }

        public TaskCategory Category 
        {
            get
            {
                return category;
            }
            set
            {
                category = value;
            }
        }

        public int TempCategory
        {
            get
            {
                return tempCategory;
            }
            set
            {
                tempCategory = value;
            }
        }

        public DateTime DueDate
        {
            get
            {
                return dueDate;
            }
            set
            {
                dueDate = value;
            }
        }

        public double GradeWeight
        {
            get
            {
                return gradeWeight;
            }
            set
            {
                if(value >= 0 && value <= 100)
                    gradeWeight = value;
            }
        }
        
        public double Grade
        {
            get
            {
                return grade;
            }
            set
            {
                grade = value;
            }
        }

        #endregion

        public String TurnInURL
        {
            get
            {
                return String.Format("http://my.neumont.edu/education/tasks/task-turn-in?task_id={0}&group_id={1}", this.Id, this.Course.Id);
            }
        }

        #region constructors

        /// <summary>
        /// Creates a new TaskItem
        /// </summary>
        public TaskItem()
        {
        }

        /// <summary>
        /// Creates a new TaskItem
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="name">Display Name</param>
        /// <param name="description">Description</param>
        /// <param name="url">LMS detail page URL</param>
        /// <param name="lastModified">Date last modified</param>
        /// <param name="remoteLocation">URL of associated file on LMS</param>
        /// <param name="course">Course this TaskItem is part of</param>
        /// <param name="dueDate">The Due Date of this task</param>
        /// <param name="gradeWeight">Weight of the grade of the category this task belongs to</param>
        private TaskItem(int id, String name, String description, String url, DateTime lastModified, String remoteLocation, Course course, DateTime dueDate, double gradeWeight)
            : base(id, name, description, url, lastModified, course, remoteLocation)
        {
            this.dueDate = dueDate;
            this.gradeWeight = gradeWeight;
        }

        /// <summary>
        /// Creates a new TaskItem
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="name">Display Name</param>
        /// <param name="description">Description</param>
        /// <param name="url">LMS detail page URL</param>
        /// <param name="lastModified">Date last modified</param>
        /// <param name="remoteLocation">URL of associated file on LMS</param>
        /// <param name="course">Course this TaskItem is part of</param>
        /// <param name="dueDate">The Due Date of this task</param>
        /// <param name="gradeWeight">Weight of the grade of the category this task belongs to</param>
        /// <param name="category">The category this task belongs to</param>
        /// <param name="parentTask">This TaskItem's parent task</param>
        public TaskItem(int id, String name, String description, String url, DateTime lastModified, String remoteLocation, Course course, DateTime dueDate, double gradeWeight, TaskCategory category, TaskItem parentTask) 
            : this(id, name, description, url, lastModified, remoteLocation, course, dueDate, gradeWeight)
        {
            this.category = category;
            this.parentTask = parentTask;
        }

        /// <summary>
        /// Creates a new TaskItem
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="name">Display Name</param>
        /// <param name="description">Description</param>
        /// <param name="url">LMS detail page URL</param>
        /// <param name="lastModified">Date last modified</param>
        /// <param name="remoteLocation">URL of associated file on LMS</param>
        /// <param name="course">Course this TaskItem is part of</param>
        /// <param name="dueDate">The Due Date of this task</param>
        /// <param name="gradeWeight">Weight of the grade of the category this task belongs to</param>
        /// <param name="category">Category ID of the category this TaskItem belongs to</param>
        /// <param name="parentTask">Task ID of the parent task of this TaskItem</param>
        public TaskItem(int id, String name, String description, String url, DateTime lastModified, String remoteLocation, Course course, DateTime dueDate, double gradeWeight, int category, int parentTask)
            : this(id, name, description, url, lastModified, remoteLocation, course, dueDate, gradeWeight)
        {
            this.tempCategory = category;
            this.tempParentTask = parentTask;
        }

        /// <summary>
        /// Creates a new TaskItem
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="name">Display Name</param>
        /// <param name="description">Description</param>
        /// <param name="url">LMS detail page URL</param>
        /// <param name="lastModified">Date last modified</param>
        /// <param name="remoteLocation">URL of associated file on LMS</param>
        /// <param name="course">Course this TaskItem is part of</param>
        /// <param name="dueDate">The Due Date of this task</param>
        /// <param name="gradeWeight">Weight of the grade of the category this task belongs to</param>
        /// <param name="category">The category this task belongs to</param>
        public TaskItem(int id, String name, String description, String url, DateTime lastModified, String remoteLocation, Course course, DateTime dueDate, double gradeWeight, TaskCategory category)
            : this(id, name, description, url, lastModified, remoteLocation, course, dueDate, gradeWeight, category, null) { }

        /// <summary>
        /// Creates a new TaskItem
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="name">Display Name</param>
        /// <param name="description">Description</param>
        /// <param name="url">LMS detail page URL</param>
        /// <param name="lastModified">Date last modified</param>
        /// <param name="remoteLocation">URL of associated file on LMS</param>
        /// <param name="course">Course this TaskItem is part of</param>
        /// <param name="dueDate">The Due Date of this task</param>
        /// <param name="gradeWeight">Weight of the grade of the category this task belongs to</param>
        /// <param name="category">Category ID of the category this TaskItem belongs to</param>
        public TaskItem(int id, String name, String description, String url, DateTime lastModified, String remoteLocation, Course course, DateTime dueDate, double gradeWeight, int category)
            : this(id, name, description, url, lastModified, remoteLocation, course, dueDate, gradeWeight, category, -1) { }

        #endregion

        #region methods

        /// <summary>
        /// Tests if this task is current
        /// </summary>
        /// <returns>True, if task is not due in the past</returns>
        public bool TaskIsCurrent()
        {
            if (DateTime.Compare(this.DueDate, DateTime.Now) < 0)
            {
                Logger.Log(this.Name + " is an old Task, ignoring...", MessageType.Debug);
                return false;
            }
            else return true;
        }

        public override void  Download()
        {
            try
            {
                if (!String.IsNullOrEmpty(this.RemoteLocation))
                {
                    string basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    base.Download(basePath + "\\sucrose\\documents\\" + this.Course.CourseNumber + "\\");
                }
            }
            catch (Exception) { }
        }

        #endregion
    }
} 